package com.ks.config.starter.controller.admin;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.ks.config.domain.dto.ConfigDO;
import com.ks.config.domain.vo.ConfigVO;
import com.ks.config.service.basic.BasicConfigService;
import com.ks.config.view.CommonReturnVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Resource
    BasicConfigService basicConfigService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Object addConfig(@RequestBody ConfigVO configVO) {
        ConfigDO configDO = new ConfigDO();
        BeanUtils.copyProperties(configVO, configDO);
        int count = basicConfigService.add(configDO);
        Assert.isTrue(count > 0, "插入失败");
        return CommonReturnVO.success();
    }

    @RequestMapping(value = "import", method = RequestMethod.POST)
    @ResponseBody
    public Object importConfig(String configJson) {
        Assert.isTrue(!StringUtils.isEmpty(configJson), "导入配置不能为空");
        List<ConfigDO> configDOS = JSON.parseArray(configJson).toJavaList(ConfigDO.class);
        configDOS.stream().forEach(configDO -> {
            basicConfigService.add(configDO);
        });
        return CommonReturnVO.success();
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Object deleteConfig(Long id) {
        Assert.isTrue(id > 0, "id 必须大于0");
        int count = basicConfigService.deleteById(id);
        Assert.isTrue(count > 0, "删除失败");
        return CommonReturnVO.success();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Object updateConfig(@RequestBody ConfigVO configVO) {
        ConfigDO configDO = new ConfigDO();
        BeanUtils.copyProperties(configVO, configDO);
        int count = basicConfigService.updateConfig(configDO);
        Assert.isTrue(count > 0, "修改失败");
        return CommonReturnVO.success();
    }

    @RequestMapping("listAll")
    public ModelAndView listAll(ModelAndView modelAndView) {
        List<ConfigDO> configDOS = basicConfigService.listAllConfig();
        modelAndView.addObject("configList", configDOS);
        modelAndView.setViewName("admin/list");
        return modelAndView;
    }

    @RequestMapping("listByKey")
    public ModelAndView listByKey(String key, ModelAndView modelAndView) {
        ConfigDO configDO = basicConfigService.listByKey(key);
        modelAndView.addObject("configList", Lists.newArrayList(configDO));
        modelAndView.setViewName("admin/list");
        return modelAndView;
    }
}
